﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GordonKristan.cppcheck
{
	class CheckModule
	{
		//Path to file or directory
		private String path;
		//True if it's a file, false if it's a directory
		private bool file;
		//Return value to hold all items
		private ListView.ListViewItemCollection items;

		/// <summary>
		/// Constructor which will set the path and then
		/// call the run function.
		/// </summary>
		/// <param name="aPath">Path to file or folder to check</param>
		/// <param name="aFile">Whether it's a file or foler</param>
		public CheckModule(String aPath, bool aFile)
		{
			path = aPath;
			file = aFile;
			runCheck();
		}

		/// <summary>
		/// Actually runs the program, then calls to parse results.
		/// </summary>
		private void runCheck()
		{
			//create the process
			System.Diagnostics.Process prog = new System.Diagnostics.Process();
			//setup the path and arguments
			prog.StartInfo.FileName = "cppcheck.exe";
			prog.StartInfo.Arguments = getArguments();
			//Make sure it redirects to our output
			prog.StartInfo.UseShellExecute = false;
			prog.StartInfo.RedirectStandardOutput = true;
			//hide the window
			prog.StartInfo.CreateNoWindow = true;
			//Start the process and grab the output
			prog.Start();
			String output = prog.StandardOutput.ReadToEnd();
			prog.WaitForExit();
			//Parse the results
			parseResults(output);
		}

		/// <summary>
		/// Function that will check the settings and create an
		/// argument string for the program based on them.
		/// </summary>
		/// <returns>Argument string to feed to program.</returns>
		private String getArguments()
		{
			return " " + path + " --template=\"{file};;{line};;{severity};;{id};;{Message}\"";
		}

		/// <summary>
		/// Parses the program output and puts in it the item collection.
		/// </summary>
		/// <param name="results"></param>
		private void parseResults(String results)
		{
			ListViewItem item = new ListViewItem();
		}

		/// <summary>
		/// Returns the items output by the program.
		/// </summary>
		/// <returns>Item collection.</returns>
		public ListView.ListViewItemCollection getItems()
		{
			return items;
		}
	}
}
